# GameState.gd - 全局游戏状态管理器
# 《光影之间》- 游戏状态和进度管理

extends Node

# 游戏状态枚举
enum GamePhase {
	MENU,
	PLAYING,
	PAUSED,
	GAME_OVER,
	LEVEL_COMPLETE
}

# 当前游戏状态
var current_phase := GamePhase.MENU
var current_level := 1
var current_chapter := 1

# 玩家数据
var player_data := {
	"name": "无名之子",
	"level": 1,
	"experience": 0,
	"shadow_vision_level": 1,
	"max_energy": 100.0,
	"current_energy": 100.0
}

# 道具收集状态
var collected_items := {}
var light_fragments := 0  # 光之碎片数量
var memory_fragments := 0  # 记忆碎片数量

# 关卡完成状态
var completed_levels := {}
var unlocked_levels := {1: true}  # 第一关默认解锁

# 故事进度
var story_flags := {}
var dialogue_seen := {}

# 设置和选项
var game_settings := {
	"master_volume": 1.0,
	"sfx_volume": 1.0,
	"music_volume": 1.0,
	"fullscreen": false,
	"vsync": true
}

# 信号
signal game_phase_changed(new_phase: GamePhase)
signal level_completed(level_number: int)
signal item_collected(item_name: String)
signal story_flag_set(flag_name: String, value: bool)

func _ready():
	print("游戏状态管理器已初始化")
	load_game_data()

# 设置游戏阶段
func set_game_phase(new_phase: GamePhase):
	if current_phase != new_phase:
		current_phase = new_phase
		game_phase_changed.emit(new_phase)
		print("游戏阶段变更为: ", GamePhase.keys()[new_phase])

# 开始新游戏
func start_new_game():
	reset_game_data()
	current_chapter = 1
	current_level = 1
	set_game_phase(GamePhase.PLAYING)
	print("开始新游戏 - 第一章：孤独觉醒")

# 重置游戏数据
func reset_game_data():
	player_data = {
		"name": "无名之子",
		"level": 1,
		"experience": 0,
		"shadow_vision_level": 1,
		"max_energy": 100.0,
		"current_energy": 100.0
	}
	collected_items.clear()
	light_fragments = 0
	memory_fragments = 0
	completed_levels.clear()
	unlocked_levels = {1: true}
	story_flags.clear()
	dialogue_seen.clear()

# 完成关卡
func complete_level(level_number: int, completion_data: Dictionary = {}):
	completed_levels[level_number] = completion_data
	
	# 解锁下一关
	if level_number + 1 <= get_max_level_for_chapter(current_chapter):
		unlocked_levels[level_number + 1] = true
	
	level_completed.emit(level_number)
	print("关卡 ", level_number, " 完成")

# 收集道具
func collect_item(item_name: String, item_data: Dictionary = {}):
	if not collected_items.has(item_name):
		collected_items[item_name] = item_data
		item_collected.emit(item_name)
		print("收集道具: ", item_name)
		
		# 特殊道具处理
		match item_name:
			"光晶石":
				light_fragments += 1
				print("获得光之碎片，当前数量: ", light_fragments)
			"记忆碎片":
				memory_fragments += 1
				print("获得记忆碎片，当前数量: ", memory_fragments)

# 检查道具是否已收集
func has_item(item_name: String) -> bool:
	return collected_items.has(item_name)

# 设置当前关卡
func set_current_level(chapter: int, level: int):
	current_chapter = chapter
	current_level = level
	print("设置当前关卡: 第", chapter, "章 第", level, "关")

# 设置故事标志
func set_story_flag(flag_name: String, value: bool):
	story_flags[flag_name] = value
	story_flag_set.emit(flag_name, value)
	print("故事标志设置: ", flag_name, " = ", value)

# 检查故事标志
func get_story_flag(flag_name: String) -> bool:
	return story_flags.get(flag_name, false)

# 标记对话已看过
func mark_dialogue_seen(dialogue_id: String):
	dialogue_seen[dialogue_id] = true

# 检查对话是否已看过
func is_dialogue_seen(dialogue_id: String) -> bool:
	return dialogue_seen.get(dialogue_id, false)

# 获取当前章节的最大关卡数
func get_max_level_for_chapter(chapter: int) -> int:
	match chapter:
		1: return 4  # 第一章有4个关卡
		2: return 5  # 第二章有5个关卡
		3: return 6  # 第三章有6个关卡
		_: return 1

# 检查关卡是否解锁
func is_level_unlocked(level_number: int) -> bool:
	return unlocked_levels.get(level_number, false)

# 获取玩家统计信息
func get_player_stats() -> Dictionary:
	return {
		"current_chapter": current_chapter,
		"current_level": current_level,
		"light_fragments": light_fragments,
		"memory_fragments": memory_fragments,
		"items_collected": collected_items.size(),
		"levels_completed": completed_levels.size()
	}

# 检查是否有存档文件
func has_save_file() -> bool:
	return FileAccess.file_exists("user://savegame.save")

# 保存游戏数据
func save_game_data():
	var save_data = {
		"player_data": player_data,
		"collected_items": collected_items,
		"light_fragments": light_fragments,
		"memory_fragments": memory_fragments,
		"completed_levels": completed_levels,
		"unlocked_levels": unlocked_levels,
		"story_flags": story_flags,
		"dialogue_seen": dialogue_seen,
		"current_chapter": current_chapter,
		"current_level": current_level,
		"game_settings": game_settings
	}
	
	var save_file = FileAccess.open("user://savegame.save", FileAccess.WRITE)
	if save_file:
		save_file.store_string(JSON.stringify(save_data))
		save_file.close()
		print("游戏数据已保存")

# 加载游戏数据
func load_game_data():
	var save_file = FileAccess.open("user://savegame.save", FileAccess.READ)
	if save_file:
		var json_string = save_file.get_as_text()
		save_file.close()
		
		var json = JSON.new()
		var parse_result = json.parse(json_string)
		
		if parse_result == OK:
			var save_data = json.data
			
			player_data = save_data.get("player_data", player_data)
			collected_items = save_data.get("collected_items", {})
			light_fragments = save_data.get("light_fragments", 0)
			memory_fragments = save_data.get("memory_fragments", 0)
			completed_levels = save_data.get("completed_levels", {})
			unlocked_levels = save_data.get("unlocked_levels", {1: true})
			story_flags = save_data.get("story_flags", {})
			dialogue_seen = save_data.get("dialogue_seen", {})
			current_chapter = save_data.get("current_chapter", 1)
			current_level = save_data.get("current_level", 1)
			game_settings = save_data.get("game_settings", game_settings)
			
			print("游戏数据已加载")
		else:
			print("保存文件解析失败")
	else:
		print("未找到保存文件，使用默认数据")
